﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YBApproval.Model;
using YBApproval.IDAL;
using YBApproval.BLL;
using System.Text;
using YBApproval.Common;

namespace YBApproval.Pages
{
    public partial class UserUpdate : System.Web.UI.Page
    {
        protected UserProfile cUser; 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (YBPSession.Instance.CurrentUser == null)
            {
                Response.Redirect(@"~/login.aspx");
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            //在该事件发生前：

            //Page 对象会针对每个控件和页调用 EnsureChildControls。 

            //设置了 DataSourceID 属性的每个数据绑定控件会调用 DataBind 方法。有关更多信息，请参见下面的数据绑定控件的数据绑定事件。

            //页上的每个控件都会发生 PreRender 事件。使用该事件对页或其控件的内容进行最后更改。

            base.OnPreRender(e);
            
            if (!IsPostBack)
            {
                InitData();
                SetUserInfoView();
                SetRolesView();
            }
        }

        private void InitData()
        {
            lblEmail.Text = YBPSession.Instance.CurrentUser.Email;
            lblLoginName.Text = YBPSession.Instance.CurrentUser.LoginName;
            lblMobile.Text = YBPSession.Instance.CurrentUser.Mobile;
            lblName.Text = YBPSession.Instance.CurrentUser.Name;
            lblPhone.Text = YBPSession.Instance.CurrentUser.Phone;

            if (YBPSession.Instance.CurrentUser.Delegate != null)
                txtDelegateId.Value = YBPSession.Instance.CurrentUser.Delegate.Value.ToString();
            else
                txtDelegateId.Value = string.Empty;

            lblDelegate.Text = "";
            txtDelegate.Text = "";
            if (YBPSession.Instance.CurUserDelegate != null)
            {
                lblDelegate.Text = YBPSession.Instance.CurUserDelegate.Name;
                txtDelegate.Text = YBPSession.Instance.CurUserDelegate.Name;
            }


            txtEmail.Text = YBPSession.Instance.CurrentUser.Email;
            txtLoginName.Text = YBPSession.Instance.CurrentUser.LoginName;
            txtMobile.Text = YBPSession.Instance.CurrentUser.Mobile;
            txtName.Text = YBPSession.Instance.CurrentUser.Name;
            txtPhone.Text = YBPSession.Instance.CurrentUser.Phone;

            using (IRole roleDAL = DALFactory.GetDAL<IRole>())
            {
                List<Role> allRole = roleDAL.GetAll();

                cblRols.DataSource = allRole;

                cblRols.DataTextField = "Name";
                cblRols.DataValueField = "Id";
                cblRols.DataBind();

                if (YBPSession.Instance.MyRoleList == null || YBPSession.Instance.MyRoleList.Count <= 0)
                    return;
                foreach (var rItem in YBPSession.Instance.MyRoleList)
                {
                    for (int i = 0; i < cblRols.Items.Count; i++)
                    {
                        if (cblRols.Items[i].Value == rItem.Id.ToString())
                            cblRols.Items[i].Selected = true;
                    }
                }
            }

        }

        private void SetUserInfoView()
        {
            txtLoginName.Visible = false;
            txtEmail.Visible = false;
            txtMobile.Visible = false;
            txtName.Visible = false;
            txtPhone.Visible = false;
            txtDelegate.Visible = false;

            lblName.Visible = true;
            lblPhone.Visible = true;
            lblMobile.Visible = true;
            lblLoginName.Visible = true;
            lblEmail.Visible = true;
            lblDelegate.Visible = true;

            btnDelegate.Visible = false;
            btnClearDelegate.Visible = false;
            hfInfoStatus.Value = "View";
        }

        private void SetUserInfoEdit()
        {
            txtLoginName.Visible = true;
            txtEmail.Visible = true;
            txtMobile.Visible = true;
            txtName.Visible = true;
            txtPhone.Visible = true;
            txtDelegate.Visible = true;

            lblName.Visible = false;
            lblPhone.Visible = false;
            lblMobile.Visible = false;
            lblLoginName.Visible = false;
            lblEmail.Visible = false;
            lblDelegate.Visible = false;

            btnDelegate.Visible = true;
            btnClearDelegate.Visible = true;
            hfInfoStatus.Value = "Edit";
        }

        private bool UpdateUserInfo()
        {
            UserProfile cUser = YBPSession.Instance.CurrentUser;
            cUser.Email = txtEmail.Text;
            cUser.LoginName = txtLoginName.Text;
            cUser.Mobile = txtMobile.Text;
            cUser.Name = txtName.Text;
            cUser.Phone = txtPhone.Text;
            cUser.Delegate = int.Parse(txtDelegateId.Value);

            using (IUserProfile userProfileDAL = DALFactory.GetDAL<IUserProfile>())
            {
                if (userProfileDAL.UpdateUserProfile(cUser))
                {
                    YBPSession.Instance.CurrentUser = cUser;
                    if (!cUser.Delegate.HasValue)
                        YBPSession.Instance.CurUserDelegate = null;
                    else
                        YBPSession.Instance.CurUserDelegate = userProfileDAL.GetUserByID(cUser.Delegate.Value);
                    InitData();
                    return true;
                }
                else
                    return false;
            }
        } 
        
        private void UpdateUserRole()
        {
            if (cblRols.Items.Count == 0)
                return;

            List<Role> newRole = new List<Role>();
            StringBuilder roleStr = new StringBuilder();
            for (int i = 0; i < cblRols.Items.Count; i++)
            {
                if (!cblRols.Items[i].Selected)
                    continue;
                newRole.Add(YBPSession.Instance.AllRolesList[i]);
                roleStr.Append(cblRols.Items[i].Value);
                roleStr.Append(",");
            }

            roleStr.Remove(roleStr.Length-1, 1);
            YBPSession.Instance.MyRoleList = newRole;
            YBPSession.Instance.CurrentUser.RoleId = roleStr.ToString();

            using (IUserProfile userDAL =  DALFactory.GetDAL<IUserProfile>())
            {
                userDAL.UpdateUserProfile(YBPSession.Instance.CurrentUser);                
            }
        }

        private void SetRolesEdit()
        {
            cblRols.Enabled = true;
            hfRoleSatus.Value = "Edit";
        }

        private void SetRolesView()
        {
            cblRols.Enabled = false;
            hfRoleSatus.Value = "View";
        }

        protected void imbEditUser_Click(object sender, ImageClickEventArgs e)
        {
            if (hfInfoStatus.Value == "View")
            {
                SetUserInfoEdit();
                imbEditUser.ImageUrl = @"~/images/Send.jpg";
                imbEditUser.ToolTip = "保存修改";
            }
            else if (hfInfoStatus.Value == "Edit")
            {
                UpdateUserInfo();
                SetUserInfoView();
                imbEditUser.ImageUrl = @"~/images/FormEdit.gif";
                imbEditUser.ToolTip = "编辑";
            }
        }

        protected void imbRoleEdit_Click(object sender, ImageClickEventArgs e)
        {
            if (hfRoleSatus.Value == "View")
            {
                SetRolesEdit();
                imbRoleEdit.ImageUrl = @"~/images/Send.jpg";
                imbRoleEdit.ToolTip = "保存修改";
            }
            else if (hfRoleSatus.Value == "Edit")
            {
                UpdateUserRole();
                SetRolesView();
                imbRoleEdit.ImageUrl = @"~/images/FormEdit.gif";
                imbRoleEdit.ToolTip = "编辑";
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Default.aspx");
        }

        protected void btnClearDelegate_Click(object sender, EventArgs e)
        {
            txtDelegate.Text = "";
            txtDelegateId.Value = "";
        }
        
    }
}